<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\AppleAccount as AppleAccountModel;
use app\common\model\Member as MemberModel;
use think\Db;
use think\Controller;

/**
 * Appleaccount 苹果账号
 * Class Appleaccount
 * @package app\home\controller
 */
class Appleaccount extends Controller
{
    /**
     * [login  苹果账号授权信息]
     * @return json
     * @author [C。]
     */
    public function login()
    {
        $param = input('param.');
        $model = new AppleAccountModel();
        $mmodel = new MemberModel();
        try{
            $appleaccount = $model->findOne(['user'=>$param['user']]);
            if(empty($appleaccount)){
                $member_data = array(
                    'username' => "善友",
                    'img' => config('home.no_user_headimg'),
                    'addtime' => time(),
                    'eqno' => 'app_apple_account',
                    'token' => md5('app_apple_account' . $param['user'] . config('home.token_halt') . time()),
                    'tokentime' => time(),
                    'sex' => 3
                );
                $memid = $mmodel->addData($member_data);
                $param['mid'] = $memid;
                $model->addData($param);
                $mmodel->editData(['id'=>$memid,'username'=>'善友'.$memid]);
                $status = 2;
            }else{
                if(empty($appleaccount['mid'])){
                    $member_data = array(
                        'username' => "善友",
                        'img' => config('home.no_user_headimg'),
                        'addtime' => time(),
                        'eqno' => 'app_apple_account',
                        'token' => md5('app_apple_account' . $param['user'] . config('home.token_halt') . time()),
                        'tokentime' => time(),
                        'sex' => 3
                    );
                    $memid = $mmodel->addData($member_data);
                    $model->editData(['id'=>$appleaccount['id'],'mid'=>$memid]);
                    $mmodel->editData(['id'=>$memid,'username'=>'善友'.$memid]);
                    $status = 2;
                }else{
                    $member_data = $mmodel->findData($appleaccount['mid'],'id','token');
                    if(empty($member_data['tel'])){
                        $status = 2;
                    }else{
                        $status = 1;
                    }
                }
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,'home/Appleaccount/login');
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '登录成功',['token'=>$member_data['token'],'status'=>$status]);
    }
    /**
     * [verifytel  验证手机]
     * @return json
     * @author [C。]
     */
    public function bindtel()
    {
        $param = input('param.');
//        $model = new AppleAccountModel();
        $mmodel = new MemberModel();
        $map = [];
        $map1 = [];
        try{
            $map['tel'] = $param['tel'];
            $map['code'] = $param['code'];

            $map1['tel'] = $param['tel'];

            $mem = Db::name('member')->where($map1)->find();
            if(!empty($mem)){
                return admin_json('-4','手机号已注册！');
            }
            $result = Db::name('vercode')->where($map)->find();
            if(!empty($result)) {
                if ($result['status'] == 2) {
                    return admin_json('-3', '验证码已使用');
                }
                if (time() - $result['addtime'] > config('home.code_expiration_time')) {
                    return admin_json('-2', '验证码已过期');
                }
                Db::name('vercode')->where($map)->update(['status' => 2]);
            }else{
                return admin_json('-1','手机号或验证码错误');
            }
            $mem = $mmodel->findData($param['token'],'token');
            $mmodel->editData(['id'=>$mem['id'],'tel'=>$param['tel']]);
//            $darr = array();
//            $nowtime = time();
//            $darr['pswd'] = md5($param['pswd']);
//            $darr['token'] = md5('app' . $param['tel'] . config('home.token_halt'));
//            $darr['tokentime'] = $nowtime;
//            $darr['eqno'] = $param['eqno'];
//            $darr['eqtype'] = $param['eqtype'];
//            $darr['username'] = $param['tel'];
//            $darr['tel'] = $param['tel'];
//            $darr['addtime'] = $nowtime;
//            $darr['img'] = config('home.no_user_headimg');
//            $id = $mmodel->addData($darr);
//            Db::name('member')->where(['id'=>$id])->update(['username'=>'善友'.$id]);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,'home/Appleaccount/bindtel');
            return admin_json(-1, '绑定失败'.$e->getMessage());
        }
        return admin_json(1, '绑定成功');
    }
    /**
     * [bindappleaccount  绑定苹果账号]
     * @return json
     * @author [C。]
     */
    public function bindappleaccount()
    {
        $param = input('param.');
        $model = new AppleAccountModel();
        $mmodel = new MemberModel();
        try{
            $member = $mmodel->findOne(['token'=>$param['token']],'id');
            $appleaccount = $model->findOne(['mid'=>$member['id']]);
            if(!empty($appleaccount)){
                return admin_json(-1, '已绑定苹果账号');
            }
            $apple = $model->findOne(['user'=>$param['user']]);
            if(!empty($apple['mid'])){
                return admin_json(-1, '该苹果账号已绑定其他用户！');
            }
            if(empty($apple)){
                $param['mid'] = $member['id'];
                $model->addData($param);
            }else{
                if(empty($apple['mid'])){
                    $param['mid'] = $member['id'];
                    $param['id'] = $apple['id'];
                    $model->editData($param);
                }

            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,'home/Appleaccount/bindappleaccount');
            return admin_json(-1, '绑定失败'.$e->getMessage());
        }
        return admin_json(1, '绑定成功');
    }
}